import ShaderLib from "../ShaderLib";

export default class RadarScanShader {
  public static getVertexShader() {
    return ShaderLib.getDefaultVertexShader();
  }
  public static getFragmentShader() {
    let shader = `
            #include <common>
            #include <fog_pars_fragment>
            #include <logdepthbuf_pars_fragment>
            varying vec2 uvTexcoord;
            uniform float time;
            uniform float radius;
            uniform vec3 v_color;      

            float d2y(float d) {
              return 1. / (0.2 + d);
            }
            float fct(vec2 p, float r, float radius){
              float a = 3.*mod(-atan(p.y, p.x)+time, 2.*PI);
              float scan = 0.*1.;
              return (d2y(a)+scan)*(1.-step(radius,r));
            }
              
            float circle(vec2 p, float r, float radius){
              float d=distance(r, radius);
              return d2y(100.*d);
            }
            
            float grid(vec2 p, float y, float radius){
              float a = 0.2;
              float res = 30.;
              float e = 0.1;
              vec2 pi = fract(p*res);
              pi = step(e, pi);
              if (length(p) > radius) {
                return 0.;
              }
              return a * y * pi.x * pi.y;
            }

            void main(){       
                vec2 uv = uvTexcoord - 0.5;

                float radius = 0.42;
                float y  = 0.;
                
                float dc = length(uv);
                
                y+=fct(uv, dc, radius); // 扇叶
                y+=circle(uv, dc, radius); // 光圈
                y+=grid(uv, y, radius); // 栅格
                gl_FragColor = vec4( y*v_color.rgb * 0.5, y);
                #include <fog_fragment>
            }
        `;
    return shader;
  }

}
